﻿using System;

namespace homework01_2
{
    class PrimeNumberDetector
    {
        static void Main1()
        {
            Console.Write("请输入下限: ");
            int lowerLimit = int.Parse(Console.ReadLine());

            Console.Write("请输入上限: ");
            int upperLimit = int.Parse(Console.ReadLine());

            Console.WriteLine($"范围内的素数（{lowerLimit}到{upperLimit}）为：");

            int count = 0;
            for (int i = lowerLimit; i <= upperLimit; i++)
            {
                if (IsPrime(i))
                {
                    Console.Write(i + "\t");
                    count++;
                    if (count % 10 == 0)
                    {
                        Console.WriteLine();
                    }
                }
            }
            if (count % 10 != 0)
            {
                Console.WriteLine();
            }
        }

        static bool IsPrime(int number)
        {
            if (number <= 1)
                return false;
            if (number == 2)
                return true; // 2是唯一的偶素数
            if (number % 2 == 0)
                return false; // 排除所有偶数

            for (int i = 3; i <= Math.Sqrt(number); i += 2)
            {
                if (number % i == 0)
                    return false;
            }
            return true;

        }
    }
}

